﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.IO;
using System.Diagnostics;

using XEvents.Library;
using WeifenLuo.WinFormsUI.Docking;


namespace ExtendedEventsManager
{
    public partial class OptionEditorForm : DockContent
    {
        #region Private Members

        private Properties.Settings s = Properties.Settings.Default;

        #endregion

        #region Constructors

        public OptionEditorForm()
        {
            InitializeComponent();
            BindOptionsPage();
        }

        #endregion

        #region Properties

        #endregion

        #region Form Methods

        private void SetMaxServerEnabled(bool enabled)
        {
            btn_ClearServerList.Enabled = enabled;
            nUD_MaxServers.Enabled = enabled;
        }

        private void SetOverrideDefaultsEnabled(bool enabled)
        {
            nUD_MaxDispatchLatency.Enabled = enabled;
            nUD_MaxEventSize.Enabled = enabled;
            nUD_MaxMemory.Enabled = enabled;
            nUD_MaxServers.Enabled = enabled;
            cmbx_EventRetentionMode.Enabled = enabled;
            cmbx_MemoryPartitionMode.Enabled = enabled;
            cmbx_StartupState.Enabled = enabled;
            cmbx_TrackCausality.Enabled = enabled;
        }

        private void BindOptionsPage()
        {
            //General Options
            chkbx_AutoConnect.Checked = s.DisplayConnect;
            chkbx_CheckVersion.Checked = s.CheckVersion;
            chkbx_CloseOpenWarning.Checked = s.CloseWithOpenWarning;
            chkbx_DropActiveWarning.Checked = s.DropActiveWarning;
            chkbx_KeepRecent.Checked = s.KeepRecent;
            nUD_MaxServers.Value = s.MaxRecent;
            SetMaxServerEnabled(chkbx_KeepRecent.Checked);


            //Default Session Options
            chkbx_OverrideDefaultOptions.Checked = s.OverrideDefaultOptions;
            SetOverrideDefaultsEnabled(chkbx_OverrideDefaultOptions.Checked);

            cmbx_EventRetentionMode.DataSource = Enum.GetNames(typeof(EventRetentionMode));
            cmbx_EventRetentionMode.SelectedItem = s.EventRetentionMode.ToString();

            cmbx_MemoryPartitionMode.DataSource = Enum.GetNames(typeof(MemoryPartitionMode));
            cmbx_MemoryPartitionMode.SelectedItem = s.MemoryPartitionMode.ToString();

            cmbx_StartupState.DataSource = Enum.GetNames(typeof(StartupState));
            cmbx_StartupState.SelectedItem = s.StartupState.ToString();

            cmbx_TrackCausality.DataSource = Enum.GetNames(typeof(TrackCausality));
            cmbx_TrackCausality.SelectedItem = s.TrackCausality.ToString();

            nUD_MaxDispatchLatency.Value = Convert.ToDecimal(s.MaxDispatchLatency);
            nUD_MaxEventSize.Value = Convert.ToDecimal(s.MaxEventSize);
            nUD_MaxMemory.Value = Convert.ToDecimal(s.MaxMemory);
        }

        #endregion

        #region Form Events

        #region Buttons

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            //General Options
            s.CheckVersion = chkbx_CheckVersion.Checked;
            s.DisplayConnect = chkbx_AutoConnect.Checked;
            s.KeepRecent = chkbx_KeepRecent.Checked;
            s.MaxRecent = (int)nUD_MaxServers.Value;
            s.DropActiveWarning = chkbx_DropActiveWarning.Checked;
            s.CloseWithOpenWarning = chkbx_CloseOpenWarning.Checked;
            if (!chkbx_KeepRecent.Checked)
                s.ServerAutoComplete.Clear();

            //Session Default Options
            s.OverrideDefaultOptions = chkbx_OverrideDefaultOptions.Checked;
            s.EventRetentionMode = (EventRetentionMode)Enum.Parse(typeof(EventRetentionMode), cmbx_EventRetentionMode.Text);
            s.MemoryPartitionMode = (MemoryPartitionMode)Enum.Parse(typeof(MemoryPartitionMode), cmbx_MemoryPartitionMode.Text);
            s.StartupState = (StartupState)Enum.Parse(typeof(StartupState), cmbx_StartupState.Text);
            s.TrackCausality = (TrackCausality)Enum.Parse(typeof(TrackCausality), cmbx_TrackCausality.Text);
            s.MaxDispatchLatency = (int)nUD_MaxDispatchLatency.Value;
            s.MaxEventSize = (int)nUD_MaxEventSize.Value;
            s.MaxMemory = (int)nUD_MaxMemory.Value;
            s.Save();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void tv_Menu_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Name == "tvMenu_Options")
            {
                this.grpbx_Options.BringToFront();
            }
            else if (e.Node.Name == "tvMenu_General")
            {
                this.grpbx_General.BringToFront();
            }
        }

        #endregion



        #endregion

        private void btn_ClearServerList_Click(object sender, EventArgs e)
        {
            s.ServerAutoComplete.Clear();
            s.Save();
        }

        private void chkbx_KeepRecent_CheckedChanged(object sender, EventArgs e)
        {
            SetMaxServerEnabled(chkbx_KeepRecent.Checked);
        }

        private void chkbx_OverrideDefaultOptions_CheckedChanged(object sender, EventArgs e)
        {
            SetOverrideDefaultsEnabled(chkbx_OverrideDefaultOptions.Checked);
        }

    }
}
